/*
 * Copyright (C) 2008  Alexander Heidrich
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/*
 * ImageCompare.java
 *
 * Created on 05.01.2009, 12:26:14
 */
package hdr_plugin;

import hdr_plugin.Exceptions.TypeNotSupportedException;
import hdr_plugin.helper.ImageJTools;
import ij.ImagePlus;
import ij.WindowManager;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;

/**
 *
 * @author Alexander Heidrich <alexander.heidrich@hki-jena.de>
 */
public class ImageCompare extends java.awt.Frame {

    /** Creates new form ImageCompare */
    public ImageCompare() {
        initComponents();
        initStackList();
    }

    private void initStackList() {
        chcStack.add("Please Select ...");
        int[] imgList = WindowManager.getIDList();
        String[] imgTitles = new String[imgList.length];
        for (int i = 0; i < imgList.length; i++) {
            ImagePlus imp = WindowManager.getImage(imgList[i]);
            if (imp != null) {
                imgTitles[i] = imp.getTitle();
            } else {
                imgTitles[i] = "";
            }
        }
        for (String title : imgTitles) {
            chcStack.add(title);
        }
        chcStack.select(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        button1 = new java.awt.Button();
        chcStack = new java.awt.Choice();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        button1.setLabel("button1");
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });
        add(button1, java.awt.BorderLayout.CENTER);
        add(chcStack, java.awt.BorderLayout.NORTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        this.dispose();
    }//GEN-LAST:event_exitForm

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
        ImagePlus imp = WindowManager.getImage(chcStack.getSelectedItem());
        for (int i = 0; i < imp.getNSlices() - 1; i++) {
            DescriptiveStatistics des = new DescriptiveStatistics();
            Object pixels = imp.getImageStack().getPixels(i + 1);
            Object pixels2 = imp.getImageStack().getPixels(i + 2);
            for (int j = 0; j < imp.getWidth() * imp.getHeight(); j++) {
                try {
                    // get pixel value at position j and the current channel
                    int m = ImageJTools.getPixelValue(pixels, j, imp.getType(), 0);
                    int n = ImageJTools.getPixelValue(pixels2, j, imp.getType(), 0);
                    des.addValue((double) n/m);
                } catch (TypeNotSupportedException ex) {
                    Logger.getLogger(ImageCompare.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
           System.out.println(des.getMean());
        }
    }//GEN-LAST:event_button1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Button button1;
    private java.awt.Choice chcStack;
    // End of variables declaration//GEN-END:variables
}
